package ctrhost

import (
	"github.com/astaxie/beego/logs"
	"fmt"
	"sort"
	"cloud_worm/util/umodels/mysqldb"
	"cloud_worm/util/umodels/mysqldb/dbmanager"
	"cloud_worm/util/uhttp"
	"cloud_worm/wormweb/controllers/ctrconfig"
)

// 应用操作相关的
type ApplyController struct {
	ctrconfig.LoginCtr
}

// NameList 获取应用名的list处理
func (a *ApplyController) NameList() {
	a.Manager()

	offset, limit := a.DealPage()
	status, err := a.GetInt("status", mysqldb.StatusAll)
	name := a.GetString("name", mysqldb.SelectStrAll)

	// 根据，获取的apply总数
	app := &dbmanager.Application{}
	count, err := app.Count(status, name)
	if err != nil {
		logs.Error(fmt.Sprintf("error in get count: %s", err.Error()))
		data := map[string]interface{}{"total": 0, "data": []dbmanager.Application{}}
		a.Data["json"] = uhttp.CreateErrResponse(uhttp.RespCodeSystemErr, err.Error(), data)
		a.ServeJSON()
		return
	}

	// 根据，获取apply具体情况：todo： Application
	apps, err := app.Select(offset, limit, status, name)
	if err != nil {
		logs.Error(fmt.Sprintf("select all apply failed: %s", err.Error()))
		data := map[string]interface{}{"total": 0, "data": []dbmanager.Application{}}
		a.Data["json"] = uhttp.CreateErrResponse(uhttp.RespCodeSystemErr, "select error.", data)
		a.ServeJSON()
		return
	}

	data := map[string]interface{}{"total": count, "data": apps}
	a.Data["json"] = uhttp.CreateOkResponse(data)
	a.ServeJSON()
	return
}

// 基于筛选条件展示所用应用
///host/apply/all?page_count=&page=&status=&Name=&
func (a *ApplyController) List() {
	a.Manager()

	offset, limit := a.DealPage()
	status, err := a.GetInt("status", mysqldb.StatusAll)			// apply状态
	name := a.GetString("name", mysqldb.SelectStrAll)				// apply 应用名 条件
	
	// 根据，获取的apply总数
	app := &dbmanager.Application{}
	count, err := app.Count(status, name)
	if err != nil {
		logs.Error(fmt.Sprintf("error in get count: %s", err.Error()))
		data := map[string]interface{}{"total": 0, "data": []dbmanager.Application{}}
		a.Data["json"] = uhttp.CreateErrResponse(uhttp.RespCodeSystemErr, err.Error(), data)
		a.ServeJSON()
		return
	}

	// 根据，获取apply具体情况：todo： Application
	apps, err := app.Application(offset, limit, status, name)
	if err != nil {
		logs.Error(fmt.Sprintf("select all apply failed: %s", err.Error()))
		data := map[string]interface{}{"total": 0, "data": []dbmanager.Application{}}
		a.Data["json"] = uhttp.CreateErrResponse(uhttp.RespCodeSystemErr, "select error.", data)
		a.ServeJSON()
		return
	}

	aList := dbmanager.ApplicationList(apps)
	sort.Sort(aList)
	data := map[string]interface{}{"total": count, "data": apps}
	a.Data["json"] = uhttp.CreateOkResponse(data)
	a.ServeJSON()
	return
}

// 展示应用的详情页面
func (a *ApplyController) Select() {
	a.Manager()

	id, err := a.GetInt("id")
	if err != nil {
		logs.Warning(fmt.Sprintf("input id error: %s", err.Error()))
		data := map[string]interface{}{"total": 0, "data": []dbmanager.Application{}}
		a.Data["json"] = uhttp.CreateErrResponse(uhttp.RespCodeParamErr, "input Apply id error", data)
		a.ServeJSON()
		return
	}

	apply := &dbmanager.Application{Id: int64(id)}
	app, err := apply.ApplicationDetailById()
	if err != nil {
		logs.Warning(fmt.Sprintf("no this id record: %s", err.Error()))
		data := map[string]interface{}{"total": 0, "data": []dbmanager.Application{}}
		a.Data["json"] = uhttp.CreateErrResponse(uhttp.RespCodeParamErr, "no input id record", data)
		a.ServeJSON()
		return
	}

	var apps []dbmanager.ApplyDetail
	apps = append(apps, *app)
	data := map[string]interface{}{"total": 1, "data": apps}
	a.Data["json"] = uhttp.CreateOkResponse(data)
	a.ServeJSON()
	return
}

// 基于application id获取应用下ip的详情
func (a *ApplyController) IPsList() {
	a.Manager()

	id, err := a.GetInt("id")
	if err != nil {
		logs.Warning(fmt.Sprintf("input id error: %s", err.Error()))
		data := map[string]interface{}{"total": 0, "data": []dbmanager.IPAndWormId{}}
		a.Data["json"] = uhttp.CreateErrResponse(uhttp.RespCodeParamErr, "input Apply id error", data)
		a.ServeJSON()
		return
	}

	m := &dbmanager.Machine{}
	ips, err := m.GetIPsByApplyID(mysqldb.StatusValid, id)
	if err != nil {
		logs.Warning(fmt.Sprintf("select for id error: %s", err.Error()))
		data := map[string]interface{}{"total": 0, "data": []dbmanager.IPAndWormId{}}
		a.Data["json"] = uhttp.CreateErrResponse(uhttp.RespCodeParamErr, "select for Apply id error", data)
		a.ServeJSON()
		return
	}

	data := map[string]interface{}{"total": len(ips), "data": ips}
	a.Data["json"] = uhttp.CreateOkResponse(data)
	a.ServeJSON()
	return
}